#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLDesTory(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}
void SLcheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int NewCapacity = ps->capacity  == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, NewCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			return 0;
		}
		ps->capacity = NewCapacity;
		ps->arr = tmp;
	}
}

void SLTailPush(SL* ps,SLDataType s)
{
	assert(ps);
	SLcheckCapacity(ps);
	ps->arr[ps->size] = s;
	++ps->size;
}
void SLpopTail(SL* ps)
{
	assert(ps->size && ps);
	--ps->size;
}
void SLFrontPush(SL* ps, SLDataType s)
{
	assert(ps);
	SLcheckCapacity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = s;
	++ps->size;
}
void SLpopFront(SL* ps)
{
	assert(ps && ps->size);
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}
int numFind(SL* ps, SLDataType s)
{
	assert(ps && ps->arr);
	for (int i = 0; i < ps->size; i++)
	{
		if (s == ps->arr[i])
		{
			return i;
		}
	}
	return -1;
}
void SLInsert(SL* ps, SLDataType s, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = s;
	++ps->size;
}

void SLpop(SL* ps, int pos)
{
	assert(ps && ps->size);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}



